package com.like.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.like.entity.DocListResp;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.ImageReq;
import com.like.entity.M;
import com.like.fragment.MainWindowFragment;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;
import com.like.utils.ImageUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ExpertsListViewAdapter extends BaseAdapter {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_EXPERT = 1;
    private Context mContext;
    private LayoutInflater inflater;
    private List<DocListResp.Doctor> mDocs;
    private int mCourseIndex = 0;
    private InfoService mInfoService;
    private M m;

    public ExpertsListViewAdapter(Context context, List<DocListResp.Doctor> docs, M m) {
        this.mContext = context;
        mInfoService = RetrofitUtil.getNoAesService(InfoService.class);
        inflater = LayoutInflater.from(context);
        mDocs = docs;
        this.m = m;
    }

    public void update(List<DocListResp.Doctor> docs, boolean append) {
        if (append)
            mDocs.addAll(docs);
        else
            mDocs = docs;
        notifyDataSetChanged();
    }

    public int getCourseIndex() {
        return mCourseIndex;
    }

    @Override
    public int getCount() {
        return mDocs.size() + 1;
    }

    @Override
    public DocListResp.Doctor getItem(int position) {
        if (getItemViewType(position) == TYPE_HEADER)
            return null;
        return mDocs.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_HEADER;
        return TYPE_EXPERT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ExpertViewHolder evh = null;
        HeaderViewHolder hvh = null;
        int viewType = getItemViewType(position);
        if (convertView == null) {
            switch (viewType) {
                case TYPE_HEADER:
                    convertView = inflater.inflate(R.layout.item_window_banner, parent, false);
                    hvh = new HeaderViewHolder(convertView);
                    convertView.setTag(hvh);
                    break;
                case TYPE_EXPERT:
                    convertView = inflater.inflate(R.layout.item_expert, null);
                    evh = new ExpertViewHolder(convertView);
                    convertView.setTag(evh);
                    break;
            }
        } else {
            switch (viewType) {
                case TYPE_HEADER:
                    hvh = (HeaderViewHolder) convertView.getTag();
                    break;
                case TYPE_EXPERT:
                    evh = (ExpertViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (viewType) {
            case TYPE_HEADER:
                break;
            case TYPE_EXPERT:
                DocListResp.Doctor doc = getItem(position);
                evh.sDocName.setText(doc.expertsName);
                evh.sDocPos.setText(doc.hospital + " " + doc.jobTitle + " " + doc.departments);
                evh.sSkills.setText(Html.fromHtml(doc.remark));
                final ImageView headImg = evh.sDocHeader;
                ImageUtils.showImage(mContext, doc.head, m.getToken(), headImg);
                break;
        }
        return convertView;
    }

    public class HeaderViewHolder {

        @ViewInject(R.id.vdo_course)
        ImageView sVdoCourse;
        @ViewInject(R.id.pro_course)
        ImageView sProCourse;
        @ViewInject(R.id.ele_book)
        ImageView sEleBook;

        private ImageView[] sViews;

        public HeaderViewHolder(View convertView) {
            x.view().inject(this, convertView);
            sVdoCourse.setSelected(true);
            sViews = new ImageView[]{sVdoCourse, sProCourse, sEleBook};
        }

        @Event({R.id.vdo_course, R.id.pro_course, R.id.ele_book})
        private void typeClick(View v) {
            for (ImageView img : sViews)
                img.setSelected(false);
            v.setSelected(true);
            switch (v.getId()) {
                case R.id.vdo_course:
                    mCourseIndex = 0;
                    break;
                case R.id.pro_course:
                    mCourseIndex = 1;
                    break;
                case R.id.ele_book:
                    mCourseIndex = 2;
                    break;
            }
            EventBus.getDefault().post(new EventWrapper(null, MainWindowFragment.class, MainWindowFragment.REQUEST_COURSE_INDEX_CHANGE));
        }

    }

    public class ExpertViewHolder {

        @ViewInject(R.id.doc_name)
        TextView sDocName;
        @ViewInject(R.id.doc_pos)
        TextView sDocPos;
        @ViewInject(R.id.doc_skills)
        TextView sSkills;
        @ViewInject(R.id.expert_header_iv)
        ImageView sDocHeader;

        public ExpertViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }
}
